package com.zm.demo.core.leetcode;

import java.util.HashMap;
import java.util.Map;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/13 10:45
 */
public class WordPattern {

    public static boolean wordPattern(String pattern, String str) {

        if (pattern == null || str == null){
            return false;
        }
        char[] c = pattern.toCharArray();
        String[]s = str.split(" ");

        if (c.length != s.length){
            return false;
        }

        Map<String,Character> map = new HashMap<>();
        Map<Character, String> map1 = new HashMap<>();
        for (int i = 0; i < c.length; i++) {
            map.putIfAbsent(s[i], c[i]);
            if (!map.get(s[i]).equals(c[i])){
                return false;
            }

            map1.putIfAbsent(c[i], s[i]);
            if (!map.get(c[i]).equals(s[i])){
                return false;
            }

        }
        return true;
    }

    public static void main(String[]args){
        out.println(wordPattern("abba", "dog1 cat cat dog"));
    }
}
